package com.briup.corejava.day17;

/**
 * 同步方法案例
 */
public class Test1 {
    public static void main(String[] args) {
        Printer p = new Printer();
        // 创建两个线程
        Thread t1 = new Thread("t1"){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    p.print1();
                }
            }
        };
        Thread t2 = new Thread("t2"){
            @Override
            public void run() {
                for (int i = 0; i < 100; i++) {
                    p.print2();
                }
            }
        };

        t1.start();
        t2.start();
    }
}
class Printer{

    // 同步方法
    public synchronized void print1(){
        System.out.print("好");
        System.out.print("好");
        System.out.print("学");
        System.out.println("习");
    }
    public void print2(){
        synchronized (this) {
            System.out.print("天");
            System.out.print("天");
            System.out.print("向");
            System.out.println("上");
        }
    }
}
